import {
    StackNavigator,
    addNavigationHelpers,
} from 'react-navigation';
import {
    createStore,
    applyMiddleware,
    combineReducers,
} from 'redux';
import {
    createReactNavigationReduxMiddleware,
    createNavigationReducer,
    createReduxBoundAddListener
} from 'react-navigation-redux-helpers';
import { Provider, connect } from 'react-redux';
import React from 'react';
import { Platform,View,StatusBar} from 'react-native';
import thunkMidlleware from 'redux-thunk';
import { persistStore, persistReducer } from 'redux-persist';
import { PersistGate } from 'redux-persist/integration/react'
import sqliteStore from './utils/sqlitestore';
import Router from './routers/index.js';
import Stores from './store.js';
import Http from './utils/http';
import sqlite,{excute} from './utils/sqlite';

const http = new Http();;
const LinkUrlFix = Platform.OS == 'android' ? 'fetail://fetail/' : 'fetail://';
const navReducer = createNavigationReducer(Router);
const persistConfig = {
    key: 'root',
    storage:sqliteStore,
    whitelist:["app"]
}
const appReducer = combineReducers({
    ...Stores,
    nav: navReducer,
});
const persistedReducer = persistReducer(persistConfig, appReducer);
const middleware = createReactNavigationReduxMiddleware(
    "root",
    state => state.nav,
);
const addListener = createReduxBoundAddListener("root");

class App extends React.Component {
    render() {
        return (
            <View style = {{flex:1}}>
                <Router 
                    screenProps={this.props.app}
                    navigation={{
                        dispatch: this.props.dispatch,
                        state: this.props.nav,
                        addListener,
                    }}
                />
            </View>
        );
    }
}

const mapStateToProps = (state) => {
    return {
        nav: state.nav
    }
};

const store = createStore(
    persistedReducer,
    applyMiddleware(middleware,thunkMidlleware),
);
const persistor = persistStore(store);
const AppWithNavigationState = connect(mapStateToProps)(App);
React.Component.prototype.$http = http;
React.Component.prototype.$sqlite = sqlite;
React.Component.prototype.$excute = excute;
class Root extends React.Component {
    render() {
        return (
            <Provider store={store}>
                <PersistGate persistor = {persistor} loading = {true} >
                    <AppWithNavigationState uriPrefix={LinkUrlFix} />
                </PersistGate>
            </Provider>
        );
    }
}
export default Root;

Date.prototype.formart = function () {
    var dt = this;
    var m = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Spt", "Oct", "Nov", "Dec");
    var w = new Array("Sunday","Monday", "Tuseday", "Wednesday", "Thursday", "Friday", "Saturday");
    var d = new Array("st", "nd", "rd", "th");
    mn = dt.getMonth();
    wn = dt.getDay();
    dn = dt.getDate();
    var dns;
    if (((dn) < 1) || ((dn) > 3)) {
        dns = d[3];
    } else {
        dns = d[(dn) - 1];
        if ((dn == 11) || (dn == 12)) {
            dns = d[3];
        }
    }
    return m[mn] + " " + dn + dns + " " + w[wn] + " " + dt.getFullYear();
}